//
//  NetworkMannger.m
//  YouthElectric
//
//  Created by lanwendong on 16/7/18.
//  Copyright © 2016年 electrical. All rights reserved.
//

#import "NetworkMannger.h"
#import <AFNetworking.h>
#import "NSString+Hash.h"
#import "CALUtilityTools.h"

//#define token @"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiI0In0.hulbzvD8m4hoyOITVXTayOC6qcrVXhl2wGfHt6jagnI"

@implementation NetworkMannger

+ (instancetype)sharedManager
{
    static NetworkMannger *sharedManagerInstance = nil;
    
    static dispatch_once_t predicate;
    
    dispatch_once(&predicate, ^{
        sharedManagerInstance = [[self alloc] init];
    });
    
    return sharedManagerInstance;
}


- (void)getShudianListWithUid:(NSString *)uid offset:(NSInteger)offset limit:(NSInteger)limit callback:(Callback)callback
{
    NSMutableDictionary *parameters = [NSMutableDictionary new];
    

    [parameters setObject:uid forKey:@"uid"];
//    [parameters setObject:@"1" forKey:@"shudian_line_id"];
//    [parameters setObject:[NSString stringWithFormat:@"%li", (long)offset] forKey:@"page_size"];
//    [parameters setObject:[NSString stringWithFormat:@"%li", (long)limit] forKey:@"page"];
    
    [self requestWithAction:@"/api/shudian/getList" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        callback(responseObject);
    } fail:^(AFHTTPRequestOperation *operation, NSError *error) {
        callback(nil);
    }];
}

#pragma mark -- 私有方法传后缀和参数进行请求
- (void)requestWithAction:(NSString *)action parameters:(NSDictionary *)parameters success:(void(^)(AFHTTPRequestOperation *operation, id responseObject))success fail:(void(^)(AFHTTPRequestOperation *operation, NSError *error))fail{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setValue:[GlobalSettings sharedInstance].token forHTTPHeaderField:@"Authentication"];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",ServerURL, action];
    [manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        success(operation, responseObject);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        fail(operation, error);
    }];
}

/**
 *  登录
 */
- (void) loginWithUser:(NSString *)user password:(NSString *)password callback:(Callback)callback
{
    NSMutableDictionary *parameters = [NSMutableDictionary new];
    
    [parameters setObject:user forKey:@"name"];
    [parameters setObject:password.md5String forKey:@"passwd"];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"application/json",nil]; //添加content type，默认情况下是不支持text／html的。
    
    NSString *url = [NSString stringWithFormat:@"%@/api/account/login",ServerURL];
    
    [manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dict = responseObject;
        if ([dict[@"status"] isEqualToString:@"success"]) {
            
            [GlobalSettings sharedInstance].token = dict[@"data"][@"token"];
            callback(dict[@"data"][@"user"]);
        }else{
            [CALUtilityTools showTips:@"登陆失败" forSecond:1.0 onView:nil];
//            callback(nil);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [CALUtilityTools showTips:error.description forSecond:1.0 onView:nil];
//        callback(nil);

    }];
    
}



@end
